﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using LuxDiner.Model.Orders;
namespace LuxDiner.Command.Comm
{
    public class Orders
    {
        /// <summary>
        /// 生成单据号
        /// </summary>
        /// <param name="db"></param>
        /// <returns></returns>
        public static string CreateOrderNo(DBCentre db, OrderMethod method)
        {
            if (db != null)
            {
                var now = DateTime.Now.Date;
                var onb = db.DataContext.ordernumbers.Where(p => p.CreatedOn.CompareTo(now) == 0 && p.OrderMethod == (int)method).FirstOrDefault();
                if (onb != null)
                    onb.Number++;
                else
                {
                    onb = new ordernumber() { Number = 1, CreatedOn = now, OrderMethod = (int)method };
                    db.DataContext.ordernumbers.Add(onb);
                }
                return string.Format("{0}-{1:yyyyMMdd}{2:0000}", method.ToString(), now, onb.Number);
            }
            return null;
        }
    }
}
